package com.finalcial.controller;


import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import jdk.nashorn.internal.ir.RuntimeNode.Request;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.finalcial.po.ApprovalJson;
import com.finalcial.po.LoginMsg;
import com.finalcial.service.ApprovalService;

@Controller
@RequestMapping("/approval")
public class ApprovalController {

	@Resource
	private ApprovalService approvalService;
	
	
	/**
	 * 待办点击同意
	 * @param approvalJson 前台接收的字段
	 * @return 修改工作状态（已办）、新增工作审批状态（同意）、新增工作状态发送给经办人(未办)
	 */
	@RequestMapping("/approvalAgreeAndBckLog")
	@ResponseBody
	public Integer approvalAgreeAndBckLog(@RequestBody ApprovalJson approvalJson,HttpSession httpSession){

		LoginMsg loginMsg = (LoginMsg) httpSession.getAttribute("LoginMsg");
		Integer userIdBySession =  loginMsg.getUserId();
		String reimbursementId = approvalJson.getReimbursementId();//报销id
		String workStateName = approvalJson.getWorkStateName();//工作状态名称
		Integer userId = approvalJson.getUserId();//用户id
		String approvalOpinion = approvalJson.getApprovalOpinion();//审批意见
	
		/*SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");//字符串转日期
		Date approvalTime = null;
		   
		try {
			approvalTime = format.parse(approvalTimes);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		  */
		String approvalStateName = approvalJson.getApprovalStateName();//审批的状态（查找的条件）
		String workStateNameUpdate  = approvalJson.getWorkStateNameUpdate();//需要修改的审批状态
		
		
		Integer total = approvalService.approvalAgreeAndBckLog(reimbursementId, workStateName, userId, approvalOpinion, approvalStateName, workStateNameUpdate, userIdBySession);
		return total;
	}
	
	
	/**
	 * 待办点击驳回
	 * @param approvalJson
	 * @return 修改工作状态(已办）/新增审批状态（未同意）
	 */
	@RequestMapping("/approvalDisAgreeAndBackLog")
	@ResponseBody
	public Integer approvalDisAgreeAndBackLog(@RequestBody ApprovalJson approvalJson,HttpSession httpSession){
		LoginMsg loginMsg = (LoginMsg) httpSession.getAttribute("LoginMsg");
		Integer userIdBySession =  loginMsg.getUserId();
		String reimbursementId = approvalJson.getReimbursementId();//报销id
		String workStateName = approvalJson.getWorkStateName();//工作状态名称
		Integer userId = approvalJson.getUserId();//用户id
		String approvalOpinion = approvalJson.getApprovalOpinion();//审批意见
		String approvalStateName = approvalJson.getApprovalStateName();//审批的状态（查找的条件）
		String workStateNameUpdate  = approvalJson.getWorkStateNameUpdate();//需要修改的审批状态
		Integer total = approvalService.approvalDisAgreeAndBackLog(reimbursementId, workStateName, approvalOpinion, approvalStateName, workStateNameUpdate, userIdBySession);
		return total;
	}
	
	
	/**
	 * 待阅--同意
	 * @param approvalJson
	 * @return 修改工作状态（已阅）/新增审批状态（同意）/新增工作状态给经办人（未办）
	 */
	@RequestMapping("/approvalAgreeAndReading")
	@ResponseBody
	public Integer approvalAgreeAndReading(@RequestBody ApprovalJson approvalJson,HttpSession httpSession){
		LoginMsg loginMsg = (LoginMsg) httpSession.getAttribute("LoginMsg");
		Integer userIdBySession =  loginMsg.getUserId();
		String reimbursementId = approvalJson.getReimbursementId();//报销id
		String workStateName = approvalJson.getWorkStateName();//工作状态名称
		Integer userId = approvalJson.getUserId();//用户id
		String approvalOpinion = approvalJson.getApprovalOpinion();//审批意见
		String approvalStateName = approvalJson.getApprovalStateName();//审批的状态（查找的条件）
		String workStateNameUpdate  = approvalJson.getWorkStateNameUpdate();//需要修改的工作状态
		String approvalStateNameUpdate = approvalJson.getApprovalStateNameUpdate();//需要修改的审批状态
		
		Integer total = approvalService.approvalAgreeAndReading(reimbursementId, workStateName, userId, approvalOpinion, approvalStateName, workStateNameUpdate, approvalStateNameUpdate, userIdBySession);
		return total;
	}
	
	/**
	 * 待阅--驳回
	 * @param approvalJson
	 * @return 修改工作状态（已阅）/新增审批状态（同意）
	 */
	@RequestMapping("/approvalDisAgreeAndReading")
	@ResponseBody
	public Integer approvalDisAgreeAndReading(@RequestBody ApprovalJson approvalJson,HttpSession httpSession){
		LoginMsg loginMsg = (LoginMsg) httpSession.getAttribute("LoginMsg");
		Integer userIdBySession =  loginMsg.getUserId();
		String reimbursementId = approvalJson.getReimbursementId();//报销id
		String workStateName = approvalJson.getWorkStateName();//工作状态名称
		String approvalOpinion = approvalJson.getApprovalOpinion();//审批意见
		String approvalStateName = approvalJson.getApprovalStateName();//审批的状态（查找的条件）
		String workStateNameUpdate  = approvalJson.getWorkStateNameUpdate();//需要修改的工作状态
		
		Integer total = approvalService.approvalDisAgreeAndReading(reimbursementId, workStateName, approvalOpinion, approvalStateName, workStateNameUpdate, userIdBySession);
		return total;
	}
}
